// 权限控制
// 1. 路由跳转权限控制
// 2. 菜单权限控制

// 这里编写路由权限控制代码
import store from '@/store'
import router from '@/router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const WHITE_LIST = ['/login', '/404'] // 白名单

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (store.state.user.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
      // 通过用户id做判断,优化重复请求
      if (!store.state.user.userInfo.userId) {
        store.dispatch('user/fetchUserInfo')
      }
    }
  } else {
    if (WHITE_LIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  NProgress.done()
})
